<?php
/**
 * 使用示例
 * @author M2
 * @version 1.0
 * create Date 2013-11-1
 * update Date 2013-11-05
 * <yl:image src="http://img0.bdstatic.com/img/image/2385ab5c9ea15ce36d3127b261938f33a87e950b198.jpg" width="100px" height="100px" alt="美女" class="" title="美女" />
 * <yl:image channelName="开发者社区" />
 * 配合其他标签一起使用
 */
class YlImageTaglib {
	public function parse($content, $arr = array(), $sid=71) {
        if (stripos($content, 'yl:image') !== false) {
            $matches = preg_parse($content, 'yl:image');
            foreach ($matches as $match) {
                $src = getParseOneAttr($match, 'src');
                $width = getParseOneAttr($match, 'width');
                $height = getParseOneAttr($match, 'height');
                $alt = getParseOneAttr($match, 'alt');
                $class = getParseOneAttr($match, 'class');
                $title = getParseOneAttr($match, 'title');
                $channelname = getParseOneAttr($match, 'channelname');
                if(empty($src)){
                    if(! empty ( $channelname )) {
                        $findChannel = M ( 'cms_category' )->where ( 'siteid='.$sid.' and cname="'.$channelname.'"' )->find ();
                        if($findChannel) {
    //                        if(substr($findChannel['imageurl'],0,7) !== 'http://' && substr($findChannel['imageurl'],0,8) !== 'https://') {
    //                            $u = SITE_URL.'/';
    //                        } else {
    //                            $u = '';
    //                        }
                            $src = $u.$findChannel['imageurl'];
                        }
                    } elseif(!empty($arr) && is_array($arr)) {
                        $src = $arr['imageurl'];
                    }
                }
                
                
                //add by tony<2014-11-18>只能是http或者是https协议开头的
                if(!empty($src)){
                    if(substr($src,0,7) !== 'http://' && substr($src, 0, 8) !== 'https://'){
                        //image的src处理
                        $first = substr($src, 0, 1);
                        if($first == "/" || $first == "~"){
                            //加上主站的域名
                            $src = SITE_URL.$src;
                        }elseif($first == "@"){
                            $src = $arr['domain'].$src;
                        }
                        if(substr($src, 0, 7) == "{upyun}"){
                            //加上又拍云的域名
                        }
                    }    
                }
                $parseStr = '<img src="' . $src . '" ';
                if (! empty ( $width ))
                    $parseStr .= 'width="' . $width . '" ';
                if (! empty ( $height ))
                    $parseStr .= 'height="' . $height . '" ';
                if (! empty ( $alt ))
                    $parseStr .= 'alt="' . $alt . '" ';
                if (! empty ( $class ))
                    $parseStr .= 'class="' . $class . '" ';
                if (! empty ( $title ))
                    $parseStr .= 'title="' . $title . '" ';
                $parseStr .= '/>';
                $content = str_replace($match, $parseStr, $content);
            }
        }
        return $content;
	}
}